﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Web;

namespace SmartSpace.News.UI.Pages
{
    public partial class EditAsset : System.Web.UI.Page
    {
        private Guid _assetId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            _assetId = Guid.Parse(Request.QueryString["assets_asset_id"]);

            if (!IsPostBack)
            {
                SetValueForUI();
            }
        }

        private void SetValueForUI()
        {
            AssetService service = new AssetService();
            Asset asset = service.GetAsset(_assetId).Data;
            if (asset != null)
            {
                _ctrlCode.Text = asset.Code;
                _ctrlGuaranteeDate.Text = asset.GuaranteeDate.ToString();
                _ctrlKeeper.Text = asset.Keeper;
                _ctrlName.Text = asset.Name;
                _ctrlPrice.Text = asset.Price.ToString();
                _ctrlRemark.Text = asset.Remark;
                _ctrlSerialNo.Text = asset.SerialNo;
                _ctrlSpecification.Text = asset.Specification;
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            AssetService service = new AssetService();
            Asset asset = service.GetAsset(_assetId).Data;
            if (asset != null)
            {
                asset.Id = Guid.NewGuid();
                asset.Code = _ctrlCode.Text;
                asset.Keeper = _ctrlKeeper.Text;
                asset.Name = _ctrlName.Text;
                asset.Price = Convert.ToDouble(_ctrlPrice.Text);
                asset.GuaranteeDate = Convert.ToDateTime(_ctrlGuaranteeDate.Text);
                asset.Remark = _ctrlRemark.Text;
                asset.SerialNo = _ctrlSerialNo.Text;
                asset.Specification = _ctrlSpecification.Text;
                service.SaveAsset(asset);
            }

            SsPage.Return(this, new { id = asset.Id });
        }
    }
}